<?php

namespace App\Http\Controllers\Admin;

use App\Model\SaRole;
use App\Service\RoleService;

class RoleController extends BaseController
{
    /**
     * 获取角色列表.
     */
    public function getRoleList()
    {
        $where['is_admin'] = $this->requestArr['is_admin'] ?? null;
        $perPage           = $this->requestArr['per_page'] ?? 15;

        $data = SaRole::getRoleList($where, $perPage);
        foreach ($data['data'] as &$val) {
            $val['status'] = 1 == $val['status'];
        }

        return successJson($data);
    }

    /**
     * 获取角色详情.
     */
    public function getRoleInfo()
    {
        $id = $this->requestArr['id'];

        $info = RoleService::getInfoById($id);

        return successJson($info);
    }

    /**
     * 添加角色.
     */
    public function createRole()
    {
        $name    = $this->requestArr['name'];
        $menuIds = $this->requestArr['menu_ids'];
        $status  = 1 == $this->requestArr['status'] ? 1 : 0;

        $result = RoleService::addRole($name, $menuIds, $this->userId, $status);

        return successJson($result);
    }

    /**
     * 编辑角色.
     */
    public function editRole()
    {
        $id      = $this->requestArr['id'];
        $name    = $this->requestArr['name'];
        $menuIds = $this->requestArr['menu_ids'];
        $status  = 1 == $this->requestArr['status'] ? 1 : 0;

        $result = RoleService::editRole($id, $name, $menuIds, $status);

        return successJson($result);
    }

    /**
     * 快捷修改角色.
     */
    public function quickEditRole()
    {
        $id             = $this->requestArr['id'];
        $data['status'] = 1 == $this->requestArr['status'] ? 1 : 0;

        $result = SaRole::editRole($id, $data);

        return successJson($result);
    }

    /**
     * 删除角色.
     */
    public function deleteRole()
    {
        $id = $this->requestArr['id'];

        $result = RoleService::delRoleById($id);

        return successJson($result);
    }
}
